package cn.bytecollege.admin.controller;

import cn.bytecollege.admin.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 商品删除，物理删除。
 */
@WebServlet(name = "ProductDeleteController", value = "/admin/deleteProduct")
public class ProductDeleteController extends HttpServlet {
    private ProductService productService = null;
    public ProductDeleteController(){
        productService = new ProductService();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Integer productId = Integer.parseInt(id);

        productService.deleteProductById(productId);
        //为了防止刷新时重复删除不存在的数据，此处用重定向跳转页面
        response.sendRedirect(request.getContextPath()+"/admin/productList");
    }
}
